package iscript.gameFramework.GameLite.directEvent
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import iscript.gameFramework.GameLite.events.TaskActionEvent;
	import iscript.gameFramework.GameLite.Game;
	
	//TODO: 加入异步事件触发	
	/**
	 * AS3 Message 是一个同步的事件触发
	 */
	public class DirectEventManager  implements IDirectEventManager
	{
		
		private var _listenerData:DirectEventListenerPool;
		private var _sendAction:DirectEventSendAction;
		private var _destroyData:DirectEventDestroyPool;
		
		public function DirectEventManager()
		{
			_listenerData = new DirectEventListenerPool();
			_destroyData = new DirectEventDestroyPool(_listenerData);
			_sendAction = new DirectEventSendAction(_listenerData);
			_sendAction.addEventListener(TaskActionEvent.COMPLETE, _destroyEventLogic);
		}
		
		/**
		 * 接受消息 == addEventListener
		 * @param	eventName
		 * @param	action
		 */
		public function receive(eventName:String, action:*):void
		{
			_listenerData.add(eventName, action);
		}
		
		/**
		 * 发送消息
		 * @param	eventName
		 * @param	eventData
		 */
		public function send(eventName:String, eventData:DirectEventParameter):void
		{
			if (_listenerData.hasEventName(eventName))
			{
				if (Game.IsDebugMode)
				trace("[DirectEvent SEND: " + eventName + " Succeed]")
				_sendAction.eventName = eventName;
				_sendAction.eventData = eventData;
				_sendAction.execute();
			}
			else
			{
				if (Game.IsDebugMode)
					trace("[DirectEvent SEND: " + eventName + " Not Register]")
			}
			
		}
		
		/**
		 * 销毁监听消息  == removeEventListener
		 * @param	eventName
		 * @param	action
		 */
		public function destroy(eventName:String, action:*):void
		{
			//监听队列有才可以放入到销毁队列
			if(_listenerData.hasEvent(eventName,action)){
				_destroyData.add(eventName, action);
			}
		}
		
		private function _destroyEventLogic(e:TaskActionEvent):void
		{
			_destroyData.remove();
		}
	}

}

